﻿// See https://aka.ms/new-console-template for more information
using System;
using System.Security.AccessControl;

namespace SimFactory
{
    public interface IShape
    {
        double GetArea();
    }

    public class Rectangle : IShape
    {
        double width;
        double height;

        public Rectangle(double width, double height)
        {
            this.width = width;
            this.height = height;
        }

        public double GetArea()
        {
            return width * height;
        }

        public double Width
        {
            get { return width; }
            set { width = value; }
        }
        public double Height
        {
            get { return height; }
            set { height = value; }
        }
    }

    public class Square : Rectangle
    {
        public Square(double len) : base(len, len) { }
    }

    public class Triangle : IShape
    {
        private double a;
        private double b;
        private double c;

        public Triangle(double a, double b, double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public double GetArea()
        {
            double p = (a + b + c) / 2;
            return Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }
        public double A
        {
            get { return a; }
            set { a = value; }
        }
        public double B
        {
            get { return b; }
            set { b = value; }
        }

        public double C
        {
            get { return c; }
            set
            {
                c = value;
            }
        }
    }

    public class ShapeFactory
    {
        public IShape CreatShape(int kind)
        {
            IShape shape = null;
            double tmp1, tmp2, tmp3;
            Random random= new Random();
            tmp1 = (double)random.Next(1, 11);
            tmp2 = (double)random.Next(1, 11);
            tmp3 = (double)random.Next(1, 11);
            switch(kind)
            {
                case 0:
                    shape =  new Rectangle(tmp1, tmp2);
                    break;
                case 1:
                    shape =  new Square(tmp1);
                    break;
                case 2:
                    shape =  new Triangle(tmp1, tmp2, tmp3);   //本程序可能在此处没有解决三角形合法性的问题，所以有时候最后的输出结果可能为NaN
                    break;
                default:
                    shape = new Rectangle(tmp1, tmp2);
                    break;
            }

            return shape;
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            double sumArea = 0;
            for (int i = 1; i <= 10; ++ i)
            {
                Random random = new Random();
                ShapeFactory shapeFactory= new ShapeFactory();
                int kind = random.Next(0,3);

                IShape shape = shapeFactory.CreatShape(kind);
                sumArea += shape.GetArea();

                Console.WriteLine(sumArea);
            }

            Console.WriteLine(sumArea);
        }
    }
}
